import json

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH, DeviceInfoHelper
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import Setting, DeskTop


class PreTest_RK3568_Basic_Setting_4400(TestCase, Setting):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 1.进入桌面，点击设置-系统-日期和时间-时间
    # 2.开启/关闭24小时制
    # 3.上下滑动修改"上午、下午、时、分"后点击确定
    # 预期结果：
    # 1.可正常进入日期和时间
    # 2.开启24小时制，时间显示为24制；关闭24小时制，时间显示为上午12制和下午12制
    # 3.时间修改生效，通知栏时间显示一致

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：进入桌面，点击设置-系统-日期和时间-时间')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)
        CommonOH.wait(self.Phone1, 3)
        CommonOH.touchByText(self.Phone1, '系统')
        CommonOH.touchByText(self.Phone1, '日期和时间')
        CommonOH.wait(self.Phone1, 1)
        Step('预期结果1：可正常进入地区')
        CommonOH.checkIfTextExist(self.Phone1, '24小时制')
        CommonOH.checkIfTypeExist(self.Phone1, 'Toggle')
        CommonOH.checkIfTextExist(self.Phone1, '日期')
        CommonOH.checkIfTextExist(self.Phone1, '时间')

        Step('步骤2：开启/关闭24小时制')
        property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
        status = property_dict.get('checked')
        if status:
            CommonOH.touchByType(self.Phone1, 'Toggle')
            Step('预期结果2：开启24小时制，时间显示为24制；关闭24小时制，时间显示为上午12制和下午12制')
            property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
            status = property_dict.get('checked')
            Asserts.assert_false(status)
        else:
            CommonOH.touchByType(self.Phone1, 'Toggle')
            Step('预期结果2：开启24小时制，时间显示为24制；关闭24小时制，时间显示为上午12制和下午12制')
            property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
            status = property_dict.get('checked')
            Asserts.assert_true(status)
            CommonOH.touchByType(self.Phone1, 'Toggle')

        Step('步骤3：上下滑动修改"上午、下午、时、分"后点击确定')
        current_time = DeviceInfoHelper.getCurrentTime(self.Phone1)
        CommonOH.touchByText(self.Phone1, '时间')
        # 滑动上午、下午
        CommonOH.swipe(self.Phone1, x1=156, y1=938, x2=156, y2=840, steps=100)
        CommonOH.swipe(self.Phone1, x1=364, y1=938, x2=156, y2=840, steps=100)
        CommonOH.swipe(self.Phone1, x1=568, y1=938, x2=156, y2=840, steps=100)
        CommonOH.touchByText(self.Phone1, '确定')
        new_time = DeviceInfoHelper.getCurrentTime(self.Phone1)
        Step('预期结果3.时间修改生效，通知栏时间显示一致')
        Asserts.assert_greater(new_time, current_time)
        text = CommonOH.getTextByCondition(self.Phone1, condition='时间', relativePath='AFTER', index=1)
        text = '{:0>5}'.format(text.replace('上午', '').replace('下午', ''))
        # 下拉通知栏
        CommonOH.swipe(self.Phone1, **DeskTop.pull_down_notification_bar)
        CommonOH.checkIfTextExist(self.Phone1, text)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
